﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Services;
using HiTeachers.Utilities;

namespace HiTeachers.Web
{
    public class PageBase : Page
    {
        private const string CurrentUserKey = "CURRENT_USER_KEY";
        private const string CurrentRolesKey = "CURRENT_ROLES_KEY";
        protected string SiteUrl { get { return ConfigurationManager.AppSettings["SiteUrl"]; } }

        protected string SitePreFix = "HiTeachers";
        protected override void OnUnload(System.EventArgs e)
        {
            base.OnUnload(e);
            NHibernateSessionManager.Instance.GetSession().Flush();
        }

        protected override void OnError(System.EventArgs e)
        {
            base.OnError(e);
            NHibernateSessionManager.Instance.RollbackTransaction();
            Logger.Error(Context.Error);
        }

        protected void BindingSubject(CheckBoxList checkBoxList)
        {
            checkBoxList.DataValueField = "ID";
            checkBoxList.DataTextField = "Name";

            checkBoxList.DataSource = SubjectServices.FindAll();
            checkBoxList.DataBind();
        }

        protected IList<string> UserRoles
        {
            get
            {
                if (HttpContext.Current.Session[CurrentUserKey] == null)
                {
                    List<string> roles = CurrentUser.Roles.Select(it => it.Name).ToList();
                    HttpContext.Current.Session[CurrentUserKey] = roles;

                    return roles;
                }

                return (List<string>)HttpContext.Current.Session[CurrentUserKey];
            }
        }

        protected Teacher CurrentUser
        {
            get
            {
                if (HttpContext.Current.Session[CurrentRolesKey] == null)
                {
                    Teacher teacher = TeacherServices.GetUser(Page.User.Identity.Name);
                    HttpContext.Current.Session[CurrentRolesKey] = teacher;

                    return teacher;
                }

                return (Teacher)HttpContext.Current.Session[CurrentRolesKey];
            }
        }

        protected void BindingPaper(Literal ltrPaper, string pagingUrl, int curPage, int totalPage)
        {
            var paper = new StringBuilder();
            var startPage = ((curPage - 4) <= 0) ? 1 : curPage - 4;
            var endPage = ((startPage + 8) > totalPage) ? totalPage : startPage + 8;

            paper.Append("<div>");
            for (var i = startPage; i <= endPage; i++)
            {
                paper.Append(i == curPage ? string.Format("<strong>{0}</strong>", i) :
                    string.Format("<a href=\"{0}?p={1}\" title=\"Đến trang {1}\">{1}</a>", pagingUrl, i));
            }
            paper.Append("<div>...</div></div>");
            if (totalPage == curPage)
                paper.Append(string.Format("<a href=\"{0}?p={1}\" title=\"Đến trang cuối\">trang cuối</a>", pagingUrl, totalPage));
            else
                paper.Append(string.Format(
                    "<a href=\"{0}?p={1}\" title=\"Đến trang kế tiếp\">tiếp</a><a href=\"{0}?p={2}\" title=\"Đến trang cuối\">trang cuối</a>",
                    pagingUrl, curPage + 1, totalPage));

            ltrPaper.Text = paper.ToString();
        }

        public void BindingMetaData(string pageKey)
        {
            var htmlHead = Page.Master.FindControl("sthd") as HtmlHead;
            if (htmlHead == null) return;

            var metaKeyword = SiteConfigServices.GetByTextKey(string.Format("{0}_Keyword", pageKey));
            if (metaKeyword != null)
            {

                var key = new HtmlMeta();
                key.Name = "keywords";
                key.Content = metaKeyword.Value;

                htmlHead.Controls.AddAt(5, key);
            }

            var metaDesc = SiteConfigServices.GetByTextKey(string.Format("{0}_Description", pageKey));
            if (metaDesc != null)
            {
                var description = new HtmlMeta();
                description.Name = "Description";
                description.Content = metaDesc.Value;

                htmlHead.Controls.AddAt(6, description);
            }
        }
    }
}
